import React, { Component } from 'react';
import { Button, Form, Input, Message } from 'antd';
import FormItem from '../../FormItem/index';
import services from '../../../services/api/index';

class NewVersion extends Component {
  constructor(props) {
    super(props);
    this.state = {};
  }
  componentDidMount() {}
  // 提交
  handleSubmit = e => {
    e.preventDefault();
    this.props.form.validateFields((err, values) => {
      if (!err) {
        const { value, version, fullPath } = this.props;
        const content = encodeURIComponent(value);
        const params = {
          filePath: fullPath,
          content: content,
          versionComment: values.versionComment,
          newVersion: true,
          version
        };
        this.saveFile(params);
      }
    });
  };

  saveFile = async params => {
    const res = await services.file.saveFile(params);
    if (res.code === 0) {
      Message.success('保存成功');
      this.props.closeAllModal();
    }
  };
  render() {
    const formItems1 = [
      {
        label: {
          label: '新版本描述',
          className: 'tantuer-form-item'
        },
        name: 'versionComment',
        options: {
          initialValue: '',
          rules: [
            { required: true, message: '请输入新版本描述' },
            { max: 256, message: '最长256个字符' }
          ]
        },
        component: <Input className='large' />
      }
    ];
    return (
      <div className='tantuer-form'>
        <Form onSubmit={() => false} className='login-form'>
          <div className='tantuer-field-area'>
            {formItems1.map(item => {
              return <FormItem key={item.name} form={this.props.form} item={item} />;
            })}
          </div>
          <div className='submit-ft'>
            <Button className='modal-cancel' onClick={this.props.closeModal}>
              取消
            </Button>
            <Button type='primary' onClick={this.handleSubmit} className='login-form-button'>
              确认
            </Button>
          </div>
        </Form>
      </div>
    );
  }
}

export default Form.create()(NewVersion);
